package kr.saeildamil.androidstudy.ch08;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class FreeLine extends Activity {
	MyView mv;
	ArrayList<VertexEntity> arList;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mv = new MyView(this);
		setContentView(mv);
		arList = new ArrayList<VertexEntity>();
	}

	public class MyView extends View {
		Paint mPaint;

		public MyView(Context context) {
			super(context);
			mPaint = new Paint();
			mPaint.setColor(Color.BLACK);
			mPaint.setStrokeWidth(3);
			mPaint.setAntiAlias(true);
		}

		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			canvas.drawColor(Color.LTGRAY);

			for (int i = 0; i < arList.size(); i++) {
				if (arList.get(i).isDraw()) {
					canvas.drawLine(arList.get(i - 1).getX(), arList.get(i - 1).getY(), 
							arList.get(i).getX(), arList.get(i).getY(), mPaint);
				}
			}
		}

		public boolean onTouchEvent(MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				Toast.makeText(
						FreeLine.this,
						"Action Down ( " + event.getX() + ", " + event.getY()
								+ " )", 1000).show();
				arList.add(new VertexEntity(event.getX(), event.getY(), false));
				return true;
			}
			if (event.getAction() == MotionEvent.ACTION_MOVE) {
				Toast.makeText(
						FreeLine.this,
						"Action Move ( " + event.getX() + ", " + event.getY()
								+ " )", 1000).show();
				arList.add(new VertexEntity(event.getX(), event.getY(), true));
				invalidate(); // 위에 명령어를 다시 수행하라
				return true;
			}
			return false;
		}

	}
}
